<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>详情</title>
    <script src="./js/LiteJS/Lite.js" type="text/javascript"></script>
</head>

<body>
<div id="wrap">
    <div class="mui-content" id="content">
        <div class="mui-card">
            <!--页眉，放置标题-->
            <div class="mui-card-header mui-card-media">
                <div class="mui-media-body">
                    <p>资源名 <span id="file_name"></span></p>
                    <p>发布者 <span id="create_user"></span></p>
                    <p>发布于 <span id="create_time"></span></p>
                    <p>资源大小 <span id="file_size"></span></p>
                    <p>资源类型 <span id="file_type"></span></p>
                    <p>资源备注 <span id="remark"></span></p>
                </div>
            </div>
            <!--内容区-->
            <div class="mui-card-content mui-card-media">
                <a href="#">
                    <img src="./images/database.png" alt="xxx">
                </a>
            </div>
            <!--页脚，放置补充信息或支持的操作-->
            <div class="mui-card-footer">
                <span id="rmv" class="mui-btn mui-btn-danger mui-pull-left">删除</span>
                <span id="dwn" class='mui-btn mui-btn-primary mui-pull-right'>下载</span>
            </div>
        </div>
    </div>
</div>
<script src="./js/common.js" type="text/javascript"></script>

<script type="text/javascript">

    function _callback() {
        mui.ready(function () {
            //加载模板数据
            bindRemove();
            bindDownLoad();
            var storage_item_obj = JSON.parse(sessionStorage.getItem("storage_item") || "");
            sessionStorage.setItem("storage_item", "");
            loadData(storage_item_obj);
        });

        function bindRemove() {
            document.querySelector("#rmv").addEventListener("click", function () {
                var file_name = document.querySelector("#file_name").textContent;
                if (!file_name) {
                    return mui.alert("待删除的文件名不能为空");
                }
                $http({
                    method: "post",
                    url: "./storage/remove.yx",
                    data: {
                        fileName: file_name
                    },
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        debugger;
                        if (data && parseInt(data) > 0) {
                            mui.alert("删除成功");
                            window.location.href = context_path.concat('/list.html');
                        }
                    },
                    Error: function (msg) {
                        mui.error(msg);
                    }
                });
            });
        }

        function loadData(storage_item_obj) {
            document.querySelector("#file_name").textContent = storage_item_obj.fileName;
            document.querySelector("#create_user").textContent = storage_item_obj.createUser;
            document.querySelector("#create_time").textContent = getLocalTime(storage_item_obj.createTimestamp);
            document.querySelector("#file_size").textContent = storage_item_obj.fileSize;
            document.querySelector("#file_type").textContent = storage_item_obj.fileType;
            document.querySelector("#remark").textContent = storage_item_obj.remark;
        }

        function bindDownLoad() {
            document.querySelector("#dwn").addEventListener("click", function () {
                var fileName = document.querySelector("#file_name").textContent;
                downLoadNow(fileName);
            });
        }

        function downLoadNow(fileName) {
            openNewWin("./storage/download.yx?fileName=" + encodeURI(fileName) + "&createUser=alex", '_blank', fileName, true);
        }

        /**
         * a模拟window.open，不会被浏览器拦截
         * @param {String} url        a标签打开的地址
         * @param {String} id         a标签的ID
         * @param {String} targetType a标签点击打开的方式（当前页面打开还是新窗口打开）
         */
        function openNewWin(url, targetType, id, _download) {
            // targetType = '_blank', id = 'open', _download = false
            // 如果存在则删除
            if (document.getElementById(id)) {
                document.body.removeChild(document.getElementById(id))
            }
            var a = document.createElement('a');
            a.setAttribute('href', url);
            if (_download) {
                a.setAttribute('download', url);
            }
            a.setAttribute('target', targetType);
            a.setAttribute('id', id);
            document.body.appendChild(a);
            a.click()
        }
    }

</script>

</body>

</html>